package com.source.barcodeMachineGrantAndRecovery.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineGrantAndRecovery.VO.BarcodeMachineRevokeVO;
import com.source.barcodeMachineGrantAndRecovery.entity.BarcodeMachineRevoke;
import com.source.barcodeMachineGrantAndRecovery.service.BarcodeMachineRevokeService;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 脚标机回收记录信息管理控制器
 * </p>
 *
 * @author Feng
 * @since 2021-02-24
 */

@Api(tags = "脚标机回收记录信息管理")
@RestController
@CrossOrigin
@RequestMapping("/barcode-machine-revoke")
public class BarcodeMachineRevokeController {

    @Resource
    private BarcodeMachineRevokeService grantService;

    @ApiOperation(value = "查询全部数据")
    @GetMapping
    public CommonResult<List<BarcodeMachineRevokeVO>> find() {

        List<BarcodeMachineRevokeVO> list = grantService.find();

        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/page")
    public CommonResult<Page<BarcodeMachineRevokeVO>> find(@RequestParam(defaultValue = "1") Integer current,
                                                           @RequestParam(defaultValue = "10") Integer size) {

        Page<BarcodeMachineRevokeVO> page = grantService.find(current, size);


        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public CommonResult<BarcodeMachineRevokeVO> find(@PathVariable Integer id) {

        BarcodeMachineRevokeVO barcodeMachineRevoke = grantService.find(id);


        return new CommonResult<>(ResultEnum.SUCCESS, barcodeMachineRevoke);

    }

    @ApiOperation(value = "根据脚标机的id查询")
    @GetMapping("/list-by-barcode-machine-id/{id}")
    public CommonResult<List<BarcodeMachineRevokeVO>> findByBarcodeMachineId(@PathVariable Integer id) {

        if (id == null) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }

        List<BarcodeMachineRevokeVO> list = grantService.findByBarcodeMachineNumber(id);

        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "根据脚标机的id查询 分页")
    @GetMapping("/page-by-barcode-machine-id/{id}")
    public CommonResult<Page<BarcodeMachineRevokeVO>> findByBarcodeMachineId(@PathVariable Integer id, @RequestParam(defaultValue = "1") Integer current,
                                                                             @RequestParam(defaultValue = "10") Integer size) {

        if (id == null) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }

        Page<BarcodeMachineRevokeVO> page = grantService.findByBarcodeMachineNumber(id, current, size);

        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "增加/修改 数据")
    @PostMapping
    public CommonResult<String> saveOrUpdate(@RequestBody BarcodeMachineRevoke barcodeMachineRevoke) {

        if (grantService.saveOrUpdate(barcodeMachineRevoke)) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation("根据id删除数据")
    @DeleteMapping("/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {

        if (id == null) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }

        if (grantService.removeById(id)) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation("批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/delete-batch")
    public CommonResult<String> delete(String ids) {    //注意：参数的分隔符逗号是英文的

        if (grantService.remove(ids)) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation("模糊查询")
    @GetMapping("/like")
    public CommonResult<List<BarcodeMachineRevokeVO>> like(String str) {

        if (StringUtils.isEmpty(str)) {
            return find();
        }

        List<BarcodeMachineRevokeVO> list = grantService.like(str);

        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation("模糊查询并分页")
    @GetMapping("/like-page")
    public CommonResult<Page<BarcodeMachineRevokeVO>> like(String str, @RequestParam(defaultValue = "1") Integer current,
                                                           @RequestParam(defaultValue = "10") Integer size) {

        if (StringUtils.isEmpty(str)) {
            return find(current, size);
        }

        Page<BarcodeMachineRevokeVO> page = grantService.like(str, current, size);

        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }
}

